<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>贵美商城-商品购买页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
-->
 <link rel="stylesheet"	type="text/css"	href="css/global.css"	/>
 <link rel="stylesheet"	type="text/css"	href="css/layout.css"	/>
 <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/JavaScript" defer="true">
  
  $(function(){
  		$("#count").change(function(){
  			var count = parseInt($("#count").val());
  			var stock = parseInt($("#count").parent().next().next().val());
  			if(!/^\d+$/.test(count)){
  				$("#count").val(1);
  			}
  			if(count > stock){
  				$("#count").val(stock);
  			}
  		});
  });
  
   function mySubmit(){
   
       var recipients = $("#recipients").val();
       var address = $("#address").val();
       var phone = $("#phone").val();
       
       if(recipients.length= 0 || address.length==0 || phone.length==0){
          alert("收件人，地址，手机不能为空");
          return false;
       }else{
          return true;
       }
       
      
   }
  
  function openWindow(){
    var address=window.showModalDialog("address.jsp","","dialogWidth=340px;dialogHeight=270px");
	  document.getElementById("address").value=address;	
		}
  function priceCalc(){
  	var oTr=document.getElementById("priceTable").getElementsByTagName("tr");
  	/**
  	* 因为priceTable从第三个tr开始出现价格列表，倒数第一行结束，所以i=2，i<trObject.length-1
  	* fSum为折扣后总价
  	* fTotal为原总价
  	*/
    var fSum=0;
    var fTotal=0;
		var j=1;
  	for (var i=2;i<oTr.length-1;i++){
  	  var oTd=oTr[i].getElementsByTagName("td");
  	  var fPrice=parseFloat(oTd[j].getElementsByTagName("span")[0].innerHTML.substring(1));
			var nCount=parseInt(oTd[j+3].getElementsByTagName("input")[0].value);
			var fRealPrice=fPrice*parseInt(oTd[j+2].getElementsByTagName("span")[0].innerHTML)/100;
  	  fSum  +=fRealPrice*nCount;
			fTotal+=fPrice*nCount;
  	  //oTd[j+1].getElementsByTagName("span")[0].innerHTML='￥'+formatFloat(fRealPrice);
      }  	 
    //oTr[oTr.length-1].getElementsByTagName("td")[1].innerHTML='￥'+formatFloat(fSum);
    //oTr[oTr.length-1].getElementsByTagName("td")[2].innerHTML='你共节省：￥'+formatFloat(fTotal-fSum);
  	}
  	function deleteItem(oThis,itemId){
  	   
  	      $.ajax({
		         data:{"itemId":itemId},
		         type:"POST",
		         url:"deleteItemAction",
		         success:function(){
	               
             }
         })
  	    
  		oFather=oThis.parentNode;
  		oFather.parentNode.parentNode.removeChild(oFather.parentNode);
  		priceCalc();
		}
		
	
//	window.onload=priceCalc();
</script>
</head>
  
  <body>
 <div id="container">
	<iframe id="header" src="header.jsp" width="980" height="136" frameborder="0" scrolling="no"></iframe>
    <div class="buy">
			<h4><img src="images/mycart.gif" alt="alt" /> <a href="#">全场运费一律2元</a> <a href="#" class="b" onclick="JavaScript:window.open('calc.htm','计算器','width=200,height=200,toolbar=no,scrollbars=no,menubar=no,screenX=100,screenY=100')">简易计算器</a></h4>
  <form id="buyForm" method="post" action="buyProductAction" name="byForm" onsubmit="return mySubmit()">
      <table class="buy" cellpadding="0" cellspacing="0" >
    	<tbody id="priceTable">
	<tr class="buytitle"><td colspan="6" class="b">确认商品价格与交易条件</td></tr>
      <tr>
        <td class="w364 b">&nbsp;&nbsp;&nbsp;&nbsp;商品名</td>
        <td class="w100 b">原价</td>
        <td class="w100 b">优惠价</td>
        <td class="w100 b">打折</td>
        <td class="w100 b">数量</td>
        <td class="w81 b">删除</td>
      </tr>
      <c:if test="${empty itemList }">
      	<tr style="text-align: center;"><td colspan="6"><img alt="" src="images/nocart.jpg"></td></tr>
      </c:if>
      <c:if test="${!empty itemList }">
      <c:forEach items="${itemList }" var="item">
       <tr class="h26 blue">
        <td><a href="showProductInfo?productId=${item.product.productId }">&nbsp;&nbsp;&nbsp;&nbsp;${item.product.productName}</a></td>
        <td><span class="c9">${item.product.originalPrice }</span></td>
        <td><span class="red">${item.product.originalPrice * item.product.discount / 10 }</span></td>
        <td><span class="black">
        		<c:if test="${item.product.discount == 10}">无</c:if>
        		<c:if test="${item.product.discount != 10}">${item.product.discount }折</c:if>
        	</span>
        </td>
        <td>
	        <input name="count" id="count" type="text" value="${item.count }" maxlength="4" size="3"/>
        </td>
        <td><a href="JavaScript:void(0)" onclick='deleteItem(this,${item.itemId})'>删除</a></td>
        <input type="hidden" id="stock" value="${item.product.stock }">
        <input type="hidden" name="itemId"  value="${item.itemId}">
     </tr>
      </c:forEach>
        </c:if>
    </tbody>
  	</table>
    <table class="buy">    
    <tbody>    
      <tr class="buytitle">
        <td colspan="2" >补充您的邮件地址和联系人基本信息</td>
      </tr>
      <tr>
				<td class="a_r"><label for="username">收件人：</label></td>
        <td><input type="text" name="recipients" id="recipients" /></td>
			</tr>
      <tr>
  		  <td class="a_r"><label for="address">地&nbsp;&nbsp;&nbsp;址： </label></td>
        <td id="selectPlace">
        	<input type="text" name="address" id="address" />
        	<a href="javascript:openWindow()" class="b">请点击填写地址</a>
  			</td>
      </tr>      
      <tr>
				<td class="a_r"><label for="tel">手&nbsp;&nbsp;&nbsp;机：</label></td>
        <td><input type="text" name="phone"  id="phone"/></td>
      </tr>
      <tr>
        <td class="h65">&nbsp;</td>
        <td><input class="imginput" type="image" src="images/submit.gif" /></td>
      </tr>
      </tbody>
  </table>
  </form>    		
		<iframe id="footer" src="footer.jsp" width="980" height="150" frameborder="0" scrolling="no"></iframe>
	</div><!--container	end-->
</div>
  </body>
</html>
